<?php
namespace Mob \ Controller;

use Think \ Controller;
use Think \ Hook;

class OperateController extends BaseController {



     public function continueTask()
     {
     	if(IS_POST)  //判断当前是否为通过post方式提交。
     	{
     	$TaskSerialno = I('Serialno');  //取任务流水号
     	$Uid = get_uid();// 取用户ID
     	$chk = $D('Task')->checkStatus($TaskSerialno,12);
     	if(!$chk)// 判断当前任务状态是否为确认状态
     	{
     	$data['status']=0;
     	$data['info']='任务在进行中了！';

     	}else
      	{
        $operate = D('Operate');


     	}
       $this->ajaxReturn($data);
       print_r($data);


     	}else
     	{
     	$this->error('系统操作异常!');
     	}




     }
   /*
    * cancelTask 取消任务操作
    *
    *
    */


    public function cancelTask()
    {
    $TaskSerialno = I('TaskId');  // 取任务流水号
     $Task = D('Task');
    if(IS_POST)
    {


     $Uid = get_uid();// 取用户UID。

    $IsAck = $Task->IsAck($TaskSerialno);  //取任务 是否已经选择受理状态
    $OperateLog =D('OperateLog');
    $operate['BusinessSerialno']=$TaskSerialno;
    $operate['OperateUid']=$Uid;
    $operate['OperateType']=12; //操作状态为取消任务
    if(!$TaskSerialno)
    {
     $data['status']=0;
     $data['info']='传递参数异常!';
     $operate['OperateFlg']=0;

    }
    elseif($IsAck)  //如果任务已经选择受理人了， 页面做跳转到申请取消界面
    {
    $AcceptId = $Task->QueryTask($TaskSerialno)['ackAcceptSerialno'];
     $this->redirect('Operate/requestCancel',array('Serialno'=>$TaskSerialno,'AcceptId'=>$AcceptId));

    }else
    {
    $status = $Task->QueryTask($TaskSerialno)['taskStatus'];  //取任务发布状态。
    if($status==9)  //判断任务是否已经被删除
    {
     $data['status']=0;
     $data['info']='任务已经删除!';
     $operate['OperateFlg']=0;

    }elseif($status==8)  //判断任务是否已经被取消。
    {
     $data['status']=0;
     $data['info']='任务已经取消!';
     $operate['OperateFlg']=0;
    }
     $oper = D('Operate'); //实例化操作模型
     $cancelReason = I('canCelReason');  //获取取消原因
    $cancelDescribe = I('Describe');     //获取取消描述

     $cancelid= $oper->cancelTask($TaskSerialno,$Uid,$cancelReason,$cancelDescribe);  //创建取消操作

    if($cancelid)
    {
    $result = $Task->cancelOperate($TaskSerialno,$Uid,$cancelid);
    $data['status']=1;
   $data['info']='任务已经取消!';

    $operate['OperateFlg']=1;
    }else
    {
    $data['status']=0;
   $data['info']='操作失败!';
 //   $data['info']=M()->getLastSql();
    $operate['OperateFlg']=0;
    }
   $OperateLog->add($operate);  //记录操作日志
    }


    $data = json_encode($data);
     print_r($data);


    }else
    {
    $TaskDetail = $Task->QueryTask($TaskSerialno);  //取任务信息。
    $this->assign('TaskDetail',$TaskDetail);
    $canCelReason = D('DataDict')->getDataItem('MOB.CANCEL.REASON'); //获取取消原因

    $this->assign('canCelReason',$canCelReason);
    $this->display();

    }

    }

     /**
	 * 申请取消任务
	 */
    public function requestCancelTask()
    {
   $TaskSerialno = I('TaskId');  // 取任务流水号
    $Task = D('Task');
    if(IS_POST)
    {
     $Uid = get_uid();// 取用户UID。
    $OperateLog =D('OperateLog');
    $operate['BusinessSerialno']=$TaskSerialno;
    $operate['OperateUid']=$Uid;
    $operate['OperateType']=113; //操作状态为取消任务
    if(!$TaskSerialno)
    {
     $data['status']=0;
     $data['info']='传递参数异常!';
     $operate['OperateFlg']=0;

    }else
    {
    $TaskInfo = D('Task')->QueryTask($TaskSerialno);  //取任务信息
    $cancelId = $TaskInfo['cancelId'];   //取任务信息中cancelid信息
    if($TaskInfo['taskStatus']==8)
    {
     $data['status']=0;
     $data['info']='任务已经关闭!';
     $operate['OperateFlg']=0;

    }elseif($TaskInfo['taskStatus']==9)
    {
    $data['status']=0;
     $data['info']='任务已删除!';
     $operate['OperateFlg']=0;

    }else{

     if(empty($cancelId))  //如果没有申请
     {
     $cancelDescribe = I('Describe');     //获取取消描述
     $oper = D('Operate');
     $cancelId=  $oper->requestCancelTask($TaskSerialno,$Uid,$cancelDescribe);   //请求取消操作
     if($cancelId)
     {
     $data['taskStatus']=12;   //更新任务状态为 申请取消状态
     $data['cancelId']=$cancelId;

    $result= $Task->UpdateTask($TaskSerialno,$Uid,$data);
    if($result)
    {
      $data['status']=1;
     $data['info']='申请成功!';
     $operate['OperateFlg']=1;

    }else
    {
      $data['status']=0;
     $data['info']='操作失败!';
     $operate['OperateFlg']=0;

    }

     }else
     {

     $data['status']=0;
     $data['info']='系统操作异常!';
     $operate['OperateFlg']=0;
     }



     }else
     {
     $data['status']=0;
     $data['info']='已经申请过了!';
     $operate['OperateFlg']=0;
     }
    }
    }

     $OperateLog->add($operate);  //记录操作日志
     $data = json_encode($data);
     print_r($data);

    }else
    {
    $TaskDetail = $Task->QueryTask($TaskSerialno);  //取任务信息。
    $this->assign('TaskDetail',$TaskDetail);
    $this->display();

    }
    }


    public function   confirmCancelTask()
    {






    }








}



